/**
 * @Author:卿雨欣
 * @Date:2020/6/18
 * @Description:这是符合策略条件的基金列表组件
 */
import React, { Component } from "react";
import {Table, Button, Modal,Card} from "antd";
import axios from "axios";
import {URL} from '../../../index'

//声明全局变量、常量
//表格的表头
const fundColumns = [
    {
        title: '名称',
        dataIndex: 'fdShareName',
    },
    {
        title: '代码',
        dataIndex: 'fdShareTsCode',
    },
    {
        title: '基金类型',
        dataIndex: 'fdShareType',

    },
    {
        title: '投资类型',
        dataIndex: 'fdShareInvestType',

    },
    {
        title: '规模(元)',
        dataIndex: 'fdShareScale',
        defaultSortOrder: 'descend',
        sorter: (a, b) => a.fdShareScale - b.fdShareScale,
    }
];
// 将获取的数据中属性为数字的转换为文字
function numberName(newList){
    let num = 0;
    while(num<newList.length) {
        switch (newList[num].fdShareType) {
            case 1:
                newList[num].fdShareType ="股票型" ;
                break;
            case 2:
                newList[num].fdShareType ="混合型" ;
                break;
            case 3:
                newList[num].fdShareType ="债券型" ;
                break;
            default:
                newList[num].fdShareType ="货币市场型" ;
                break;
        }
        switch (newList[num].fdShareInvestType) {
            case 5:
                newList[num].fdShareInvestType = "被动指数型";
                break;
            case 6:
                newList[num].fdShareInvestType = "灵活配置型";
                break;
            case 3:
                newList[num].fdShareInvestType = "债券型";
                break;
            case 2:
                newList[num].fdShareInvestType = "混合型";
                break;
            default:
                newList[num].fdShareInvestType = "货币型";
        }
        num++;
    }
    return newList;
};
export default class FundTable extends Component{
    constructor(props) {
        super(props);
        this.state = {
            selectedRowKeys: [], // 选中的行的key
            loading: false,
            selectedRows:[],//选中的行
            selectedNames:" ",//选中的产品的名称平凑的字符串
            fdShareList:[], //后端获取到的基金数组
            fdShareLength:0, //策略列表规定的基金产品数量
        };
    }
    componentWillMount() {
        var sId=parseInt(this.props.strategyIdFund) //从父组件获取的策略id
        // 获取符合策略条件的产品列表
        axios({
            method:"GET",
            url:URL+"/strategy/strategyFiltrate",
            params:{strategyId:sId},
        }).then(res=>{
            if(res.data.code == 500){
                this.props.history.push('/serverError')
            }else{
                //更新声明的状态
                this.setState({
                    fdShareList:numberName(res.data.fdShareList), //使用函数，将数字转化为文字
                    fdShareLength:res.data.fdShareLength,
                })
                this.props.onFundsLength(res.data.fdShareLength) // 给父组件传值，策略规定的基金产品数量
            }
        }).catch(error=>{
            console.log(error);
        });
    }

    //重置函数
    start = (selectedRows) => {
        this.setState({ loading: true });
        setTimeout(() => {
            this.setState({
                selectedRowKeys: [],
                loading: false,
                selectedNames:" ",
            });
        }, 1000);
        this.props.onFunds([]);
    };

    // 选择列表项，保存选中的项
    onSelectChange = (selectedRowKeys,selectedRows,selectedNames,fdShareList)=> {
        //选择的数量小于策略规定的基金数量
        if(selectedRowKeys.length<=this.state.fdShareLength){
            let names = ""; //变量，拼接的选中产品的名称
            //拼接选中的产品的名称
            for (let i = 0; i < selectedRowKeys.length; i++) {
                let item = selectedRowKeys[i];
                for (let i2 = 0; i2 < this.state.fdShareList.length; i2++) {
                    if (this.state.fdShareList[i2].fdShareId === item) {
                        names += "【" + this.state.fdShareList[i2].fdShareName + "】" + " ";
                    }
                }
            }
            this.setState({
                selectedRowKeys,
                selectedRows,
                selectedNames:names,
            });
            this.props.onFunds(selectedRows);
        }else{
            Modal.warning({
                title: '基金数量提示',
                content: '该策略基金数量为'+this.state.fdShareLength+",请选择该数量的基金",
            })
        }
    }
    render() {
        //声明render中用到的变量、常量
        const { loading, selectedRowKeys } = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
        };
        const hasSelected = selectedRowKeys.length > 0;
        return (
            <div>
                <div style={{width:"45%",float:"left",marginLeft:"5%"}}>
                    <div style={{ marginBottom: 16}}>
                        <Button type="primary" onClick={this.start} disabled={!hasSelected} loading={loading}>
                            清除
                        </Button>
                        <span style={{ marginLeft: 8 }}>
                            {hasSelected ? `已选 ${selectedRowKeys.length} 支基金` : ''}
                            {hasSelected ? ` ${ this.state.selectedNames} ` : ''}
                        </span>
                    </div>
                    <Table rowSelection={rowSelection}
                           pagination={{pageSize:5}}
                           columns={fundColumns}
                           dataSource={this.state.fdShareList}
                           rowKey="fdShareId" />
                </div>
            </div>
        )
    }
}
